guard
, when
, and unless
allow for conditional operations and short-cutting in monadic expressions.
Guards
Guards are used to stop the monadic expression continuing if a flag is true
(for guard
) or false
(for guardnot
).
They only work with monads that have an 'alternative value' (which is usually used as the error condition: Left
in
Either
for example). An alternative value is provided when the guard triggers:
from x in ma
from _ in guard(x == 100, Error.New("x should be 100"))
select x;
Supported monads are:
Either
EitherUnsafe
EitherAsync
Fin
Validation
Aff
Eff
When and Unless
when
and unless
are similar to guards, but instead of providing the alternative value, you provide an alternative monad
to run. This monad could be in a failed state, or it could run a successful side effect (an Aff
calling Console<RT>.writeLine()
for example).
from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;
Contents
- Prelude
- guard <E> (bool flag, Func<E> False)
- guard <E> (bool flag, E False)
- guard (bool flag, Func<Error> False)
- guard (bool flag, Error False)
- guardnot <E> (bool flag, Func<E> True)
- guardnot <E> (bool flag, E True)
- guardnot (bool flag, Func<Error> True)
- guardnot (bool flag, Error True)
- Prelude
- unless <RT> (bool flag, Aff<RT, Unit> alternative)
- unless <RT> (bool flag, Eff<RT, Unit> alternative)
- unless (bool flag, Aff<Unit> alternative)
- unless (bool flag, Eff<Unit> alternative)
- unless (bool flag, Try<Unit> alternative)
- unless (bool flag, TryAsync<Unit> alternative)
- unless (bool flag, TryOption<Unit> alternative)
- unless (bool flag, TryOptionAsync<Unit> alternative)
- unless (bool flag, Option<Unit> alternative)
- unless (bool flag, OptionUnsafe<Unit> alternative)
- unless (bool flag, OptionAsync<Unit> alternative)
- unless <L> (bool flag, Either<L, Unit> alternative)
- unless <L> (bool flag, EitherUnsafe<L, Unit> alternative)
- unless <L> (bool flag, EitherAsync<L, Unit> alternative)
- unless (bool flag, Fin<Unit> alternative)
- unless (bool flag, Task<Unit> alternative)
- unless (bool flag, ValueTask<Unit> alternative)
- unless <L> (bool flag, Validation<L, Unit> alternative)
- unless <MonoidL, L> (bool flag, Validation<MonoidL, L, Unit> alternative)
- Prelude
- when <RT> (bool flag, Aff<RT, Unit> alternative)
- when <RT> (bool flag, Eff<RT, Unit> alternative)
- when (bool flag, Aff<Unit> alternative)
- when (bool flag, Eff<Unit> alternative)
- when (bool flag, Try<Unit> alternative)
- when (bool flag, TryAsync<Unit> alternative)
- when (bool flag, TryOption<Unit> alternative)
- when (bool flag, TryOptionAsync<Unit> alternative)
- when (bool flag, Option<Unit> alternative)
- when (bool flag, OptionUnsafe<Unit> alternative)
- when (bool flag, OptionAsync<Unit> alternative)
- when <L> (bool flag, Either<L, Unit> alternative)
- when <L> (bool flag, EitherUnsafe<L, Unit> alternative)
- when <L> (bool flag, EitherAsync<L, Unit> alternative)
- when (bool flag, Fin<Unit> alternative)
- when (bool flag, Task<Unit> alternative)
- when (bool flag, ValueTask<Unit> alternative)
- when <L> (bool flag, Validation<L, Unit> alternative)
- when <MonoidL, L> (bool flag, Validation<MonoidL, L, Unit> alternative)
Methods
method Guard<E> guard <E> (bool flag, Func<E> False) Source #
Guard against continuing a monadic expression
Parameters
param | flag | Flag for continuing |
param | False | If the flag is false, this provides the error |
returns | Guard |
method Guard<E> guard <E> (bool flag, E False) Source #
Guard against continuing a monadic expression
Parameters
param | flag | Flag for continuing |
param | False | If the flag is false, this provides the error |
returns | Guard |
method Guard<Error> guard (bool flag, Func<Error> False) Source #
Guard against continuing a monadic expression
Parameters
param | flag | Flag for continuing |
param | False | If the flag is false, this provides the error |
returns | Guard |
method Guard<Error> guard (bool flag, Error False) Source #
Guard against continuing a monadic expression
Parameters
param | flag | Flag for continuing |
param | False | If the flag is false, this provides the error |
returns | Guard |
method Guard<E> guardnot <E> (bool flag, Func<E> True) Source #
Guard against continuing a monadic expression
Parameters
param | flag | Flag for continuing |
param | True | If the flag is false, this provides the error |
returns | Guard |
method Guard<E> guardnot <E> (bool flag, E True) Source #
Guard against continuing a monadic expression
Parameters
param | flag | Flag for continuing |
param | True | If the flag is false, this provides the error |
returns | Guard |
Methods
method Aff<RT, Unit> unless <RT> (bool flag, Aff<RT, Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;
method Eff<RT, Unit> unless <RT> (bool flag, Eff<RT, Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;
method Aff<Unit> unless (bool flag, Aff<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;
method Eff<Unit> unless (bool flag, Eff<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;
method Try<Unit> unless (bool flag, Try<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, Try(() => WriteLineAsync<RT>("x should be 100!")))
select x;
method TryAsync<Unit> unless (bool flag, TryAsync<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, TryAsync(() => WriteLineAsync<RT>("x should be 100!")))
select x;
method TryOption<Unit> unless (bool flag, TryOption<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, TryOption(() => WriteLineAsync<RT>("x should be 100!")))
select x;
method TryOptionAsync<Unit> unless (bool flag, TryOptionAsync<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, TryOptionAsync(() => WriteLineAsync<RT>("x should be 100!")))
select x;
method Option<Unit> unless (bool flag, Option<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, None)
select x;
method OptionUnsafe<Unit> unless (bool flag, OptionUnsafe<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, None)
select x;
method OptionAsync<Unit> unless (bool flag, OptionAsync<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, None)
select x;
method Either<L, Unit> unless <L> (bool flag, Either<L, Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, Left<string, int>("100 is the only value accepted"))
select x;
method EitherUnsafe<L, Unit> unless <L> (bool flag, EitherUnsafe<L, Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, LeftUnsafe<string, int>("100 is the only value accepted"))
select x;
method EitherAsync<L, Unit> unless <L> (bool flag, EitherAsync<L, Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, LeftAsync<string, int>("100 is the only value accepted"))
select x;
method Fin<Unit> unless (bool flag, Fin<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, FinFail(Error.New("100 is the only value accepted"))
select x;
method Task<Unit> unless (bool flag, Task<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, async () => await LaunchMissiles())
select x;
method ValueTask<Unit> unless (bool flag, ValueTask<Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, async () => await LaunchMissiles())
select x;
method Validation<L, Unit> unless <L> (bool flag, Validation<L, Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, Fail<string, Unit>("100 is the only value accepted"))
select x;
method Validation<MonoidL, L, Unit> unless <MonoidL, L> (bool flag, Validation<MonoidL, L, Unit> alternative) Source #
Run the alternative
when the flag
is false
, return Pure Unit
when true
Parameters
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
Examples
from x in ma
from _ in unless(x == 100, Fail<MString, string, Unit>("100 is the only value accepted"))
select x;
Methods
method Aff<RT, Unit> when <RT> (bool flag, Aff<RT, Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;
method Eff<RT, Unit> when <RT> (bool flag, Eff<RT, Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;
method Aff<Unit> when (bool flag, Aff<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;
method Eff<Unit> when (bool flag, Eff<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;
method Try<Unit> when (bool flag, Try<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, Try(() => WriteLine<RT>("x is 100, finally!")))
select x;
method TryAsync<Unit> when (bool flag, TryAsync<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, TryAsync(() => WriteLineAsync<RT>("x is 100, finally!")))
select x;
method TryOption<Unit> when (bool flag, TryOption<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, TryOptionAsync(() => WriteLineAsync<RT>("x is 100, finally!")))
select x;
method TryOptionAsync<Unit> when (bool flag, TryOptionAsync<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, TryOptionAsync(() => WriteLineAsync<RT>("x is 100, finally!")))
select x;
method Option<Unit> when (bool flag, Option<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, None)
select x;
method OptionUnsafe<Unit> when (bool flag, OptionUnsafe<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, None)
select x;
method OptionAsync<Unit> when (bool flag, OptionAsync<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, None)
select x;
method Either<L, Unit> when <L> (bool flag, Either<L, Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, Left<string, int>("Won't accept 100"))
select x;
method EitherUnsafe<L, Unit> when <L> (bool flag, EitherUnsafe<L, Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, LeftUnsafe<string, int>("Won't accept 100"))
select x;
method EitherAsync<L, Unit> when <L> (bool flag, EitherAsync<L, Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, LeftAsync<string, int>("Won't accept 100"))
select x;
method Fin<Unit> when (bool flag, Fin<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, FinFail(Error.New("Won't accept 100")))
select x;
method Task<Unit> when (bool flag, Task<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, async () => await LaunchMissiles())
select x;
method ValueTask<Unit> when (bool flag, ValueTask<Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, async () => await LaunchMissiles())
select x;
method Validation<L, Unit> when <L> (bool flag, Validation<L, Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, Fail<string, Unit>("100 isn't accepted"))
select x;
method Validation<MonoidL, L, Unit> when <MonoidL, L> (bool flag, Validation<MonoidL, L, Unit> alternative) Source #
Run the alternative
when the flag
is true
, return Pure Unit
when false
Parameters
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
Examples
from x in ma
from _ in when(x == 100, Fail<MString, string, Unit>("100 isn't accepted"))
select x;